<template>
  <div>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="$parent.open" width="780px" append-to-body>
      <div class="center_dialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="分类级别" prop="type" v-if="title !== '修改分类' && title !== '新增分类'">
            <el-select v-model="form.type" placeholder="请选择分类级别" @change="() => form.parentId = ''" style="width:100%">
              <el-option v-for="(dict, index) in ['一级分类', '二级分类']" :key="index" :label="dict" :value="index + ''">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="form.type === '1'" label="上级分类" prop="parentId">
            <el-select v-model="form.parentId" @change="handlerParent" placeholder="请选择上级分类" style="width:100%">
              <el-option v-for="(dict, index) in typeList" :key="index" :label="dict.typeName" :value="dict.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分类名称" prop="typeName">
            <el-input v-model="form.typeName" placeholder="请输入类分类名称" />
          </el-form-item>
          <el-form-item label="归属" prop="ascription" v-if="form.type === '0' && title !== '修改分类' && title !== '新增分类'">
            <el-select v-model="form.ascription" placeholder="请选择归属" style="width:100%">
              <el-option v-for="(dict, index) in ascArr" :key="index" :label="dict" :value="index + ''">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="form.sort" style="width: 100%;" :min="1" controls-position="right">
            </el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <div align="center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="() => $parent.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getinspectiontypeList, Updateinspectiontype, Createdinspectiontype } from "@/api/system/difftype.js";
// import Upload from '@/components/upload/index.vue'
export default {
  props: ['title', 'rowObj'],
  components: {
    // Upload
  },
  data () {
    return {

      form: {
        type: '',
        "parentId": "",     //父级分类id  二级分类必传
        "typeName": "",   //分类名称
        "ascription": "",   //归属（1机房日常巡检 2线上巡检记录 3设备维护记录 4值班记录）
        "sort": undefined      //排序
      },
      typeList: [],
      rules: {
        type: { required: true, message: '必填', trigger: ['blur', 'change'] },
        parentId: { required: true, message: '必填', trigger: ['blur', 'change'] },
        typeName: { required: true, message: '必填', trigger: ['blur', 'change'] },
        ascription: { required: true, message: '必填', trigger: ['blur', 'change'] },
        sort: { required: true, message: '必填', trigger: ['blur', 'change'] },
      }
    }
  },
  computed: {
    ascArr () {
      return {
        '1': '机房日常巡检',
        '2': '线上巡检记录',
        '3': '设备维护记录',
        '4': '值班记录'
      }
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    async getlist () {
      let { data: data } = await getinspectiontypeList({ parentId: 0 });
      this.typeList = data
      if (this.title === '修改分类') {//修改进入
        this.form = { ...this.rowObj }
      }
      if (this.title === '新增分类') {
        this.form.type = '0'
        this.form.parentId = this.rowObj.id
        this.form.ascription = this.rowObj.ascription
      }
    },
    handlerParent (id) {
      this.typeList.forEach(element => {
        if (element.id === id) {
          this.form.ascription = element.ascription
        }
      });
    },
    //提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '修改分类') {//修改
            let params = {
              ...this.form
            }
            Updateinspectiontype(params).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.$parent.open = false
                this.$parent.$parent.$refs['search'].handleSelect()
                // this.$parent.getList()//重新查询列表
              }
            })
          } else {//新增
            let params = {
              ...this.form
            }
            Createdinspectiontype(params).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.$parent.open = false
                if (this.title === '新增分类') {
                  this.$parent.getList()//重新查询列表
                } else {
                  this.$parent.handleUpdate()

                }
              }
            })
          }
        }
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.center_dialog {
  // text-align: center;
  width: 50%;
  margin: 0px auto;
}
</style>